const Koa = require("koa");
const IO = require("socket.io");
const fs = require("fs");
const path = require("path");
const bus = require("./bus");
const config = require("../config/index");
const https = require("https");
const creteLocalCert = require("../lib/creteLocalCert");


creteLocalCert()


const certPath  = path.resolve(config.sslCaDir, "./certs/localhost.pem")
const keyPath = path.resolve(config.sslCaDir, "./keys/localhost.key")
// const certPath  = path.resolve(__dirname, "../localhost-cert.cert")
// const keyPath = path.resolve(__dirname, "../localhost-key.key")

console.log('keyPath',keyPath)
console.log('keyPath',certPath)

const options = {
  key:fs.readFileSync(keyPath), // 替换为你的私钥文件路径
  cert:fs.readFileSync(certPath), // 替换为你的证书文件路径
};
// console.log('optionsoptions',options)
const koaStatic = require("koa-static");
const app = new Koa();
// app.use(async (ctx, next) => {
 
//     await next();
// });
const server = https.createServer(options, app.callback());
const socketIO = IO(server);
socketIO.on("connection", (socket) => {
  bus.on("request", (url) => {
    socket.emit("log", url);
  });
});
app.use(koaStatic("./web"));
function createServer(callback) {
  server.listen(config.webPort, callback);
}
// createServer(()=>{})
module.exports = {
  createServer,
};
